<?php $url = new Url();?>
<div class="left">
    <?php $this->widget("AdminQuanly")?>
</div>
<div class="right-main">
    <div class="righ-cont">
        <div class="right">
            <div class="title_style"> <span><b>
            <?php if(!isset($row["id"])){?>
            Thêm mới Admin
            <?php }else{?>
            Đổi mật khẩu admin
            <?php }?>
            </b></span></div>

            <div class="table clearfix" style="border: none;">
                <table cellspacing="0" cellpadding="0" border="0">
                    <tbody>
                        <tr class="bg_gray">
                            <td><b>STT</b></td>
                            <td><strong>Admin</strong></td>
                            <td><strong>Mật khẩu</strong></td>
                            <td><strong>Nhập Lại Mật khẩu</strong></td>
                            <td>Save</td>
                        </tr>

                        <tr>
                            <td>&nbsp;<input id="user_id" name="user_id" type="hidden" value="<?=isset($row["id"]) ? $row["id"]:""?>"></td>                          
                            <td><input type="text" id="username" name="username" value="<?=isset($row["username"]) ? $row["username"]:""?>"/></td>
                            <td><input type="password" id="password" name="password" <?=isset($row["password"]) ? "disable='disable'":""?>/></td>
                            <td><input type="password" id="re_pass" name="re_pass" <?=isset($row["password"]) ? "disable='disable'":""?>/></td>
                            <?php if(!isset($row["id"])){?>
                                <td><span id="save"></span><input type="button" value="Save" onclick="addAdmin();"/></td>
                            <?php }else{?>
                                <td><span id="save"></span><input type="button" value="Save" onclick="update('<?=$row["id"] ?>');"/></td>
                            <?php }?>
                        </tr>

                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    function addAdmin(){
        var username = $("#username").val();
        var password = $("#password").val();
        var re_pass = $("#re_pass").val(); 
        if(username == ""){
            alert("Ten tai khoan admin khong duoc de trong!");
            return false;
        }
        if(password ==""){
            alert("Mat khau khong duoc de trong");
            return false;
        }
        if(password != re_pass){
            alert("Mat khau nhap lai khong chinh xac");
            return false
        }
        var strUrl = '<?=$url->createUrl("admin/create") ?>'
        $.ajax({
            type: "POST",
            url: strUrl,
            data: {
                username:username,
                password:password
            },
            success: function(msg){
                //alert(msg);
                $("#save").removeClass("loading");
                if(msg==1){                   
                    $("#save").html("Loi");             
                }else if(msg==2){
                    alert("Da ton tai admin ten nay!");
                    return false;
                }else if(msg==3){
                    window.location = '<?=$url->createUrl("admin/index")?>';
                }
            },
            beforeSend:function(){
                $("#save").html("");
                $("#save").addClass("loading");
            }
        });
    }
    function update(id){
        var password = $("#password").val();
        var re_pass = $("#re_pass").val(); 
        if(password ==""){
            alert("Mat khau khong duoc de trong");
            return false;
        }
        if(password != re_pass){
            alert("Mat khau nhap lai khong chinh xac");
            return false
        }
        var strUrl = '<?=$url->createUrl("admin/update") ?>';
        $.ajax({
            type: "POST",
            url: strUrl,
            data: {
                id:id,
                password:password
            },
            success: function(msg){
                //alert(msg);
                $("#save").removeClass("loading");
                if(msg==1){       
                    alert('Sửa thành công');            
                    window.location = '<?=$url->createUrl("admin/index")?>';             
                }else{
                    alert('Có lỗi');
                    return false;
                }
            },
            beforeSend:function(){
                $("#save").html("");
                $("#save").addClass("loading");
            }
        });
    }
</script>